{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入pyecharts库\n",
    "import pyecharts\n",
    "\n",
    "# 导入pandas库，用于处理数据\n",
    "import pandas as pd\n",
    "\n",
    "# numpy库，用于处理数据\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>房型</th>\n",
       "      <th>价格</th>\n",
       "      <th>小区</th>\n",
       "      <th>面积（㎡）</th>\n",
       "      <th>建造年份</th>\n",
       "      <th>户型</th>\n",
       "      <th>朝向</th>\n",
       "      <th>装修类型</th>\n",
       "      <th>楼层</th>\n",
       "      <th>区域</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>大华电梯两房/房型正气/开门南北通/房东诚意出售</td>\n",
       "      <td>76531</td>\n",
       "      <td>大华锦绣华城(十六街区)(公寓)</td>\n",
       "      <td>90.16</td>\n",
       "      <td>2010</td>\n",
       "      <td>2室2厅</td>\n",
       "      <td>南</td>\n",
       "      <td>简装</td>\n",
       "      <td>中楼层(共18层)</td>\n",
       "      <td>浦东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>非底楼 满五年唯一 税费少 婚房装修 楼称佳 户型方正</td>\n",
       "      <td>52290</td>\n",
       "      <td>芳雅苑</td>\n",
       "      <td>63.11</td>\n",
       "      <td>1995</td>\n",
       "      <td>2室1厅</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>低楼层(共6层)</td>\n",
       "      <td>浦东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>满五唯一+7号线锦绣路+复式房+带阁楼+小区央位+精装</td>\n",
       "      <td>62878</td>\n",
       "      <td>锦博苑</td>\n",
       "      <td>79.52</td>\n",
       "      <td>2007</td>\n",
       "      <td>2室2厅</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>高楼层(共6层)</td>\n",
       "      <td>浦东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13号线陈春路地铁400米中间楼层诚意卖看房方便</td>\n",
       "      <td>45866</td>\n",
       "      <td>鹏海小区</td>\n",
       "      <td>71.95</td>\n",
       "      <td>1997</td>\n",
       "      <td>2室1厅</td>\n",
       "      <td>南</td>\n",
       "      <td>简装</td>\n",
       "      <td>中楼层(共6层)</td>\n",
       "      <td>浦东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>朝阳正气一房，采光好，坐看花园，户型方正，看房方便</td>\n",
       "      <td>83942</td>\n",
       "      <td>万邦都市花园</td>\n",
       "      <td>54.80</td>\n",
       "      <td>2004</td>\n",
       "      <td>1室1厅</td>\n",
       "      <td>南</td>\n",
       "      <td>简装</td>\n",
       "      <td>中楼层(共11层)</td>\n",
       "      <td>浦东</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66010</th>\n",
       "      <td>富丽苑 2室2厅 275万</td>\n",
       "      <td>29743</td>\n",
       "      <td>富丽苑</td>\n",
       "      <td>92.46</td>\n",
       "      <td>2005</td>\n",
       "      <td>2室2厅</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>中楼层(共6层)</td>\n",
       "      <td>宝山</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66011</th>\n",
       "      <td>房东自住精装修，集中式空调系统加地暖，带电梯。</td>\n",
       "      <td>35650</td>\n",
       "      <td>金辉兰湖美域</td>\n",
       "      <td>182.33</td>\n",
       "      <td>2013</td>\n",
       "      <td>4室2厅</td>\n",
       "      <td>南 北</td>\n",
       "      <td>精装</td>\n",
       "      <td>高楼层(共5层)</td>\n",
       "      <td>宝山</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66012</th>\n",
       "      <td>美兰湖颐景园 2室2厅 276万</td>\n",
       "      <td>32838</td>\n",
       "      <td>美兰湖颐景园</td>\n",
       "      <td>84.05</td>\n",
       "      <td>2007</td>\n",
       "      <td>2室2厅</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>低楼层(共5层)</td>\n",
       "      <td>宝山</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66013</th>\n",
       "      <td>五楼低总价，两房朝南，满五唯一，格局好，配套齐全</td>\n",
       "      <td>26493</td>\n",
       "      <td>罗南二村</td>\n",
       "      <td>69.83</td>\n",
       "      <td>1996</td>\n",
       "      <td>2室1厅</td>\n",
       "      <td>南</td>\n",
       "      <td>简装</td>\n",
       "      <td>高楼层(共6层)</td>\n",
       "      <td>宝山</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66014</th>\n",
       "      <td>满五唯一，带电梯，精装修近地铁，户型方正楼层好</td>\n",
       "      <td>39983</td>\n",
       "      <td>美兰湖岭域</td>\n",
       "      <td>92.04</td>\n",
       "      <td>2010</td>\n",
       "      <td>2室2厅</td>\n",
       "      <td>南</td>\n",
       "      <td>精装</td>\n",
       "      <td>中楼层(共6层)</td>\n",
       "      <td>宝山</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>66015 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                房型     价格                 小区   面积（㎡）  建造年份  \\\n",
       "0         大华电梯两房/房型正气/开门南北通/房东诚意出售  76531  大华锦绣华城(十六街区)(公寓)    90.16  2010   \n",
       "1      非底楼 满五年唯一 税费少 婚房装修 楼称佳 户型方正  52290               芳雅苑    63.11  1995   \n",
       "2      满五唯一+7号线锦绣路+复式房+带阁楼+小区央位+精装  62878               锦博苑    79.52  2007   \n",
       "3         13号线陈春路地铁400米中间楼层诚意卖看房方便  45866              鹏海小区    71.95  1997   \n",
       "4        朝阳正气一房，采光好，坐看花园，户型方正，看房方便  83942            万邦都市花园    54.80  2004   \n",
       "...                            ...    ...                ...     ...   ...   \n",
       "66010                富丽苑 2室2厅 275万  29743               富丽苑    92.46  2005   \n",
       "66011      房东自住精装修，集中式空调系统加地暖，带电梯。  35650            金辉兰湖美域   182.33  2013   \n",
       "66012             美兰湖颐景园 2室2厅 276万  32838            美兰湖颐景园    84.05  2007   \n",
       "66013     五楼低总价，两房朝南，满五唯一，格局好，配套齐全  26493              罗南二村    69.83  1996   \n",
       "66014      满五唯一，带电梯，精装修近地铁，户型方正楼层好  39983             美兰湖岭域    92.04  2010   \n",
       "\n",
       "          户型     朝向  装修类型           楼层  区域  \n",
       "0      2室2厅      南    简装    中楼层(共18层)   浦东  \n",
       "1      2室1厅      南    精装     低楼层(共6层)   浦东  \n",
       "2      2室2厅      南    精装     高楼层(共6层)   浦东  \n",
       "3      2室1厅      南    简装     中楼层(共6层)   浦东  \n",
       "4      1室1厅      南    简装    中楼层(共11层)   浦东  \n",
       "...      ...    ...   ...          ...  ..  \n",
       "66010  2室2厅      南    精装     中楼层(共6层)   宝山  \n",
       "66011  4室2厅    南 北    精装     高楼层(共5层)   宝山  \n",
       "66012  2室2厅      南    精装     低楼层(共5层)   宝山  \n",
       "66013  2室1厅      南    简装     高楼层(共6层)   宝山  \n",
       "66014  2室2厅      南    精装     中楼层(共6层)   宝山  \n",
       "\n",
       "[66015 rows x 10 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取CSV格式文件，注意文件分隔要么用反斜杠/  要么用双斜\\\\\n",
    "f = open(\"Task_4_lianjia_sale.csv\", encoding=\"utf-8\")\n",
    "df = pd.read_csv(f)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### (1)利用groupby函数，以【区域】字段进行groupby，同时结合agg聚合函数统计各个区域平均房价；将统计所得平均房价命名为【区域平均价格】列；并将数据从小到大按顺序排列；最后展示所得前5行数据（将所得的DataFrame命名为df_avg）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>区域平均价格</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>区域</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>金山</th>\n",
       "      <td>19304.818824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>奉贤</th>\n",
       "      <td>23662.131268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>松江</th>\n",
       "      <td>36461.715479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>嘉定</th>\n",
       "      <td>36587.687199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青浦</th>\n",
       "      <td>37078.860149</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          区域平均价格\n",
       "区域              \n",
       "金山  19304.818824\n",
       "奉贤  23662.131268\n",
       "松江  36461.715479\n",
       "嘉定  36587.687199\n",
       "青浦  37078.860149"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用groupby函数，以【区域】字段进行groupby，同时结合agg聚合函数统计各个区域平均房价，将统计所得平均房价命名为【区域平均价格】\n",
    "# df.groupby(\"被统计的列\")[\"选择一列做运算\"].agg({\"新列名\": \"mean\"}).sort_values(by=\"新列名\")\n",
    "df_avg = df.groupby(\"区域\")[\"价格\"].agg(区域平均价格=(\"mean\")).sort_values(by=\"区域平均价格\")\n",
    "df_avg.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### （2）利用Pyecharts将第（1）问所得数据作条形图，图表标题为“上海各区域平均价格”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"8b99a241a9034199a496d9a8b66646cd\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_8b99a241a9034199a496d9a8b66646cd = echarts.init(\n",
       "                    document.getElementById('8b99a241a9034199a496d9a8b66646cd'), 'white', {renderer: 'canvas'});\n",
       "                var option_8b99a241a9034199a496d9a8b66646cd = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u533a\\u57df\\u5e73\\u5747\\u4ef7\\u683c\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                19305.0,\n",
       "                23662.0,\n",
       "                36462.0,\n",
       "                36588.0,\n",
       "                37079.0,\n",
       "                42578.0,\n",
       "                49081.0,\n",
       "                55413.0,\n",
       "                57943.0,\n",
       "                62727.0,\n",
       "                66165.0,\n",
       "                72100.0,\n",
       "                74305.0,\n",
       "                75396.0,\n",
       "                95015.0\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u533a\\u57df\\u5e73\\u5747\\u4ef7\\u683c\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u533a\\u57df\\u5e73\\u5747\\u4ef7\\u683c\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"\\u91d1\\u5c71\",\n",
       "                \"\\u5949\\u8d24\",\n",
       "                \"\\u677e\\u6c5f\",\n",
       "                \"\\u5609\\u5b9a\",\n",
       "                \"\\u9752\\u6d66\",\n",
       "                \"\\u5b9d\\u5c71\",\n",
       "                \"\\u95f5\\u884c\",\n",
       "                \"\\u6d66\\u4e1c\",\n",
       "                \"\\u666e\\u9640\",\n",
       "                \"\\u6768\\u6d66\",\n",
       "                \"\\u8679\\u53e3\",\n",
       "                \"\\u957f\\u5b81\",\n",
       "                \"\\u9759\\u5b89\",\n",
       "                \"\\u5f90\\u6c47\",\n",
       "                \"\\u9ec4\\u6d66\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u4e0a\\u6d77\\u5404\\u533a\\u57df\\u5e73\\u5747\\u4ef7\\u683c\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_8b99a241a9034199a496d9a8b66646cd.setOption(option_8b99a241a9034199a496d9a8b66646cd);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x7ff2219f92b0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入pyecharts.charts中的Bar，作条形图\n",
    "from pyecharts.charts import Bar\n",
    "# 导入配置项入口\n",
    "from pyecharts import options as opts\n",
    "\n",
    "# 将df_avg的索引即【区域】提取出来 并转换成list,作为横坐标数据\n",
    "district = df_avg.index.values.tolist()\n",
    "# 将【区域平均价格】数据值转成list 并将数值转成整数，作为纵坐标数据\n",
    "b = df_avg[\"区域平均价格\"].values.tolist()\n",
    "avg_price = np.round(b).tolist()\n",
    "\n",
    "# 构造柱形图对象\n",
    "bar = Bar()\n",
    "# 添加x轴数据\n",
    "bar.add_xaxis(district)\n",
    "# 添加y轴数据\n",
    "bar.add_yaxis(\"区域平均价格\", avg_price)\n",
    "# 对全局进行配置,对其中的标题进行设置\n",
    "bar.set_global_opts(title_opts=opts.TitleOpts(title=\"上海各区域平均价格\"))\n",
    "\n",
    "# 直接在jupytr notebook中渲染\n",
    "bar.render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### (3)利用groupby函数，以【装修类型】字段进行groupby，同时结合agg聚合函数统计各个区域房源数量；将统计所得数据命名为【房源数量】列；最后展示所得数据（将所得的DataFrame命名为df_zhuangxiu）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>房源数量</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>装修类型</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>其他</th>\n",
       "      <td>5414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>毛坯</th>\n",
       "      <td>5357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>简装</th>\n",
       "      <td>22819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>精装</th>\n",
       "      <td>32425</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       房源数量\n",
       "装修类型       \n",
       " 其他    5414\n",
       " 毛坯    5357\n",
       " 简装   22819\n",
       " 精装   32425"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用groupby函数，以【装修类型】字段进行groupby，同时结合agg聚合函数统计各个装修类型的房源数量，将统计所得数据命名为【房源数量】\n",
    "# df.groupby(\"被统计的列\")[\"选择一列做运算\"].agg({\"新列名\": \"count\"}).sort_values(by=\"新列名\")\n",
    "df_zhuangxiu = df.groupby(\"装修类型\")[\"价格\"].agg(房源数量=(\"count\"))\n",
    "df_zhuangxiu"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### （4）利用Pyecharts将第（3）问所得数据作饼图，图表标题为“装修类型占比情况”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"917e41964a0a4ec685d3580bb3eebc3b\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_917e41964a0a4ec685d3580bb3eebc3b = echarts.init(\n",
       "                    document.getElementById('917e41964a0a4ec685d3580bb3eebc3b'), 'white', {renderer: 'canvas'});\n",
       "                var option_917e41964a0a4ec685d3580bb3eebc3b = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \" \\u5176\\u4ed6 \",\n",
       "                    \"value\": 5414\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \" \\u6bdb\\u576f \",\n",
       "                    \"value\": 5357\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \" \\u7b80\\u88c5 \",\n",
       "                    \"value\": 22819\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \" \\u7cbe\\u88c5 \",\n",
       "                    \"value\": 32425\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"0%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"50%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}: {d}\"\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \" \\u5176\\u4ed6 \",\n",
       "                \" \\u6bdb\\u576f \",\n",
       "                \" \\u7b80\\u88c5 \",\n",
       "                \" \\u7cbe\\u88c5 \"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u88c5\\u4fee\\u7c7b\\u578b\\u5360\\u6bd4\\u60c5\\u51b5\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_917e41964a0a4ec685d3580bb3eebc3b.setOption(option_917e41964a0a4ec685d3580bb3eebc3b);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x7ff2239f8780>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入pyecharts.charts中的Pie，作饼图\n",
    "from pyecharts.charts import Pie\n",
    "# 导入配置项入口\n",
    "from pyecharts import options as opts\n",
    "\n",
    "\n",
    "# 将df_zhuangxiu的索引即【装修类型】提取出来，并转换成list,作为横坐标数据\n",
    "zhuangxiu = df_zhuangxiu.index.values.tolist()\n",
    "# 将【房源数量】数据值转成list，作为纵坐标数据\n",
    "house_num = df_zhuangxiu[\"房源数量\"].values.tolist()\n",
    "\n",
    "# 构造饼图对象\n",
    "pie = Pie()\n",
    "# [list(z) for z in zip(zhuangxiu, house_num)]   输出数据为[[' 毛坯 ', 5720], [' 其他 ', 5832], [' 简装 ', 24233], [' 精装 ', 34317]]\n",
    "pie.add(\"\", [list(z) for z in zip(zhuangxiu, house_num)])\n",
    "# 设置标题\n",
    "pie.set_global_opts(title_opts=opts.TitleOpts(title=\"装修类型占比情况\"))\n",
    "# 设置系列格式formatter=\"{b}: {d}\"  d代表百分比，c代表数值\n",
    "pie.set_series_opts(label_opts=opts.LabelOpts(formatter=\"{b}: {d}\"))\n",
    "\n",
    "# 直接在jupytr notebook中渲染\n",
    "pie.render_notebook()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
